package cn.jinjimi.app.poster.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import java.io.*;
import java.net.*;

import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.main.dialog.PosterGuideDialog;
import cn.jinjimi.app.main.extend.BaseActivity;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.poster.fragment.PosterContainerFragment;
import cn.jinjimi.app.poster.fragment.PosterFragment;
import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.StringUtils;

import static cn.jinjimi.app.bean.SharePrefKeys.PREF_PRODUCT_POSTER_FIRST;

/**
 * Created by lforxeverc on 16/9/22.
 */

public class ProductPosterActivity extends BaseActivity {
    private String productName;
    private String productId;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_product_poster);
    }

    @Override
    protected void initial() {
        findViewById(R.id.btn_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        productId = getIntent().getStringExtra(BundleKey.POSTER_PRODUCT_ID);
        productName = getIntent().getStringExtra(BundleKey.POSTER_PRODUCT_NAME);
        if (!StringUtils.isEmpty(productName)) {
            try {
                productName = URLDecoder.decode(productName, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        FrescoImageView imgHead = (FrescoImageView) findViewById(R.id.img_head);
        imgHead.setImageURI(Uri.parse("res:///" + R.drawable.logo));

        showPosterFragment();
    }

    private void showPosterFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        PosterFragment fragment = PosterFragment.create(PosterContainerFragment.LoadType.LOAD_CACHE_NET, null, PosterContainerFragment.DataType.PRODUCT_POSTER, -1, productId, productName);
        ft.add(R.id.fl_container, fragment);
        ft.commit();
        if (!PreferenceManager.getBooleanData(PREF_PRODUCT_POSTER_FIRST)) {
            PreferenceManager.setData(PREF_PRODUCT_POSTER_FIRST, true);
            PosterGuideDialog dialog = new PosterGuideDialog(this);
            dialog.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
